package com.linxz.sdkmanager.wechat;

import android.graphics.Bitmap;
import android.widget.Toast;

import com.linxz.framework.configuration.Linxz;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * @author Linxz
 * 创建日期：2020年01月12日 14:14
 * version：v4.5.4
 * 描述：
 */
public class WxSdkManager {
    private static final String APP_ID = "wxd2592705ee6e355a";
    private IWXAPI api;

    private WxSdkManager(){
        regToWx();
    }

    private static class Hold{
        private final static WxSdkManager INSTANCE=new WxSdkManager();
    }

    public static WxSdkManager getInstance(){
        return Hold.INSTANCE;
    }

    private void regToWx(){
        api = WXAPIFactory.createWXAPI(Linxz.getApplicationContext(), APP_ID, true);
        api.registerApp(APP_ID);
    }


    public void share(Bitmap bitmap){
        if (!api.isWXAppInstalled()) {
            Toast.makeText(Linxz.getApplicationContext(), "微信未安装", Toast.LENGTH_LONG).show();
            return;
        }
        WXWebpageObject webpage=new WXWebpageObject();
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.mediaObject = new WXImageObject(bitmap);

        SendMessageToWX.Req req=new SendMessageToWX.Req();
        req.transaction=String.valueOf(System.currentTimeMillis());
        req.message=msg;
        req.scene= SendMessageToWX.Req.WXSceneSession;

        api.sendReq(req);
    }


    /**
     * 分享网页
     * */
    public void shareArticleLink(String tittle,String url){
        if (!api.isWXAppInstalled()) {
            Toast.makeText(Linxz.getApplicationContext(), "微信未安装", Toast.LENGTH_LONG).show();
            return;
        }
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl =url;
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title =tittle;
        msg.description ="学而不用非学也！有用的话就放到你的项目里面吧。有用的话就放到你的项目里面吧。有用的话就放到你的项目里面吧。";
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.message =msg;
        req.scene = SendMessageToWX.Req.WXSceneSession;
        api.sendReq(req);
    }


}
